package com.controller;

import java.util.Arrays;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.Condition;
import com.constant.AuthConstant;
import com.domain.entity.ChongdianzhuangbaoxiuEntity;
import com.domain.entity.YonghuEntity;
import com.exception.ErrorCode.BizErrorCode;
import com.service.ChongdianzhuangbaoxiuService;
import com.utils.RedisClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.bind.annotation.*;


import com.service.YonghuService;
import com.utils.PageUtils;
import com.utils.R;


@RestController
@RequestMapping("/yonghu")
public class YonghuController {

    @Autowired
    private YonghuService yonghuService;

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private ChongdianzhuangbaoxiuService chongdianzhuangbaoxiuService;

    @Autowired
    private TransactionTemplate transactionTemplate;

    /**
     * 管理用户分页查询
     */
    @GetMapping("/page")
    public R page(@RequestParam int pageNum,@RequestParam int pageSize, YonghuEntity yonghu) {

        PageUtils page = yonghuService.queryPage(pageNum,pageSize,yonghu);

        return R.ok().put("data", page);
    }

    /**
     * 管理端用户详情查询
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {

        String json = redisClient.getString(AuthConstant.YONGHU_INFO_PREFIX + id);
        if (StringUtils.isNotEmpty(json)) {

            YonghuEntity entity = JSONObject.parseObject(json, YonghuEntity.class);
            return R.ok().put("data", entity);
        }

        YonghuEntity entity = yonghuService.selectById(id);
        if (entity != null) {
            redisClient.setMinString(AuthConstant.YONGHU_INFO_PREFIX + id, JSONObject.toJSONString(entity), 2000L);
        }

        return R.ok().put("data", entity);
    }


    /**
     * 管理端用户信息修改
     */
    @PostMapping("/update")
    public R update(@RequestBody YonghuEntity yonghu) {

        //先检查用户账号和用户姓名有没有发生变化
        YonghuEntity entity = yonghuService.selectById(yonghu.getId());
        if (entity.getUseraccount().equals(yonghu.getUseraccount()) && entity.getUsername().equals(yonghu.getUsername())) {
            //都相等只用跟新一张表
            yonghuService.updateById(yonghu);
            return R.ok();
        }
        ChongdianzhuangbaoxiuEntity chongdianzhuangbaoxiuEntity = new ChongdianzhuangbaoxiuEntity();
        chongdianzhuangbaoxiuEntity.setId(yonghu.getId());
        chongdianzhuangbaoxiuEntity.setYonghuxingming(yonghu.getUsername());
        chongdianzhuangbaoxiuEntity.setYonghuzhanghao(yonghu.getUseraccount());

        transactionTemplate.execute(status -> {
            try {
                yonghuService.updateById(yonghu);
                //不相等还要跟新另一张表
                chongdianzhuangbaoxiuService.update(chongdianzhuangbaoxiuEntity, new Condition()
                        .eq("yonghuid", yonghu.getId()));

            } catch (Exception e) {
                //回滚
                status.setRollbackOnly();
                return R.error(BizErrorCode.HTTP_SERVER_ERROR.getMessage());
            }
            return R.ok();
        });

        return R.ok();
    }


    /**
     * 管理端用户信息删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        yonghuService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }


}
